let fmoney2 = (value) => {
  var n = 2;
  value = parseFloat((value + '').replace(/[^\d\.-]/g, '')).toFixed(n) + '';
  var l = value.split('.')[0].split('').reverse();
  var r = value.split('.')[1];
  var t = '';
  var i = 0;
  for (var j = 0; j < l.length; j++) {
    t += l[j] + ((i + 1) % 3 === 0 && (i + 1) !== l.length ? ',' : '');
  };
  value = t.split('').reverse().join('') + '.' + r;
  return value;
};

let ftime = (datetime) => {
  if (datetime != null) {
    var date = new Date(datetime);
    var y = date.getFullYear();
    var mon = date.getMonth() + 1;
    var d = date.getDate();
    var h = date.getHours();
    var min = date.getMinutes();
    var s = date.getSeconds();
    mon = mon < 10 ? ('0' + mon) : mon;
    d = d < 10 ? ('0' + d) : d;
    h = h < 10 ? ('0' + h) : h;
    min = min < 10 ? ('0' + min) : min;
    s = s < 10 ? ('0' + s) : s;
    return y + '-' + mon + '-' + d + ' ' + h + ':' + min + ':' + s;
  };
};

let ftimeDay = (datetime) => {
  if (datetime != null) {
    var date = new Date(datetime);
    var y = date.getFullYear();
    var mon = date.getMonth() + 1;
    var d = date.getDate();
    mon = mon < 10 ? ('0' + mon) : mon;
    d = d < 10 ? ('0' + d) : d;
    return y + '-' + mon + '-' + d;
  };
};
let ftimeDay2 = (datetime) => {
  if (datetime != null) {
    var date = new Date(datetime);
    var y = date.getFullYear();
    var mon = date.getMonth() + 1;
    var d = date.getDate();
    mon = mon < 10 ? ('0' + mon) : mon;
    d = d < 10 ? ('0' + d) : d;
    return y + '年' + mon + '月' + d + '日';
  };
};

let fmon = (datetime) => {
  if (datetime != null) {
    var date = new Date(datetime);
    var y = date.getFullYear();
    var mon = date.getMonth() + 1;
    var d = date.getDate();
    mon = mon < 10 ? ('0' + mon) : mon;
    d = d < 10 ? ('0' + d) : d;
    return y + '年' + mon + '月';
  };
};

let digitUppercase = (n) => {
  var fraction = ['角', '分'];
  var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  var unit = [
    ['元', '万', '亿'],
    ['', '拾', '佰', '仟']
  ];
  var head = n < 0 ? '欠' : '';
  n = Math.abs(n);
  var s = '';
  for (var i = 0; i < fraction.length; i++) {
    s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
  };
  s = s || '整';
  n = Math.floor(n);
  for (var z = 0; z < unit[0].length && n > 0; z++) {
    var p = '';
    for (var j = 0; j < unit[1].length && n > 0; j++) {
      p = digit[n % 10] + unit[1][j] + p;
      n = Math.floor(n / 10);
    }
    s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][z] + s;
  }
  return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
};

let fmoney = (value) => {
  if (!value) return '0.00';
  // value = value/100;
  /*原来用的是Number(value).toFixed(0)，这样取整时有问题，例如0.51取整之后为1，感谢Nils指正*/
  value = value / 100
  var intPart = Number(value) | 0; //获取整数部分
  var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); //将整数部分逢三一断
  var floatPart = ".00"; //预定义小数部分
  var value2Array = value.toString().split(".");
  //=2表示数据有小数位
  if (value2Array.length == 2) {
    floatPart = value2Array[1].toString(); //拿到小数部分
    if (floatPart.length == 1) { //补0,实际上用不着
      return intPartFormat + "." + floatPart + '0';
    } else {
      return intPartFormat + "." + floatPart;
    }
  } else {
    return intPartFormat + floatPart;
  }
}
let fseconds = (value)=> {
  var secondTime = parseInt(value); // 秒
  var minuteTime = 0; // 分
  var hourTime = 0; // 小时
  if (secondTime > 60) { //如果秒数大于60，将秒数转换成整数
    //获取分钟，除以60取整数，得到整数分钟
    minuteTime = parseInt(secondTime / 60);
    //获取秒数，秒数取余，得到整数秒数
    secondTime = parseInt(secondTime % 60);
    //如果分钟大于60，将分钟转换成小时
    if (minuteTime > 60) {
      //获取小时，获取分钟除以60，得到整数小时
      hourTime = parseInt(minuteTime / 60);
      //获取小时后取余的分，获取分钟除以60取余的分
      minuteTime = parseInt(minuteTime % 60);
    }
  }
  let result = "" + parseInt(secondTime) + "秒";

  if (minuteTime > 0) {
    result = "" + parseInt(minuteTime) + "分钟" + result;
  }
  if (hourTime > 0) {
    result = "" + parseInt(hourTime) + "小时" + result;
  }
  return result;
};
export {
  fmoney,
  ftime,
  ftimeDay,
  ftimeDay2,
  digitUppercase,
  fmoney2,
  fmon,
  fseconds,
};